package com.test;

/*
import java.util.ArrayList;

public class Example02 {
    public static void main(String[] args){
        ArrayList list=new ArrayList();
        list.add("zhangsan");
        list.add("lisi");
        list.add("wangwu");
        list.add("zhaoliu");
        System.out.println("集合的长度:"+list.size());
        System.out.println("第二个元素是:"+list.get(1));
        list.remove(3); // 删除索引为3的元素
        System.out.println("删除索引为3的元素:"+list);
        list.set(1,"sili2");
        System.out.println("替换索引为1的元素为lisi2:"+list);
    }
}


 */

/*
import java.util.LinkedList;

public class Example02 {
    public static void main(String[] args){
        LinkedList link=new LinkedList();
        link.add("zhangsan");
        link.add("lisi");
        link.add("wangwu");
        link.add("zhaoliu");
        System.out.println(link.toString());
        link.add(3,"Student");  //索引3处加入Student
        link.addFirst("First");     //第一个位置加入First
        System.out.println(link);
        System.out.println(link.getFirst());    //取出来第一个元素
        link.remove(3);
        link.removeFirst();
        System.out.println(link);
    }
}

 */

/*
import java.util.ArrayList;
import java.util.Iterator;

public class Example02 {
    public static void main(String[] args){
        ArrayList list=new ArrayList();
        list.add("张三");                     // 向该集合中添加字符串
        list.add("李四");
        list.add("王五");
        list.add("赵六");
        Iterator it = list.iterator();
        while(it.hasNext()){
            Object obj=it.next();
            System.out.println(obj);
        }
    }
}


 */

/*
import java.util.ArrayList;

public class Example02 {
    public static void main(String[] args){
        ArrayList list=new ArrayList();
        list.add("张三");			        // 向ArrayList集合中添加字符串元素
        list.add("李四");
        list.add("王五");
        for(Object obj:list){
            System.out.println(obj);
        }
    }
}

 */

/*
public class Example02 {
    static String[] strs = {"aaa", "bbb", "ccc"};

    public static void main(String[] args) {
        //foreach
        for (String str : strs) {
            str = "ddd";
        }
        System.out.println("foreach循环修改后的数组:" + strs[0] + "," + strs[1] + "," + strs[2]);
        //for
        for (int i = 0; i < strs.length; i++) {
            strs[i] = "ddd";
        }
        System.out.println("普通for循环修改后的数组:" + strs[0] + "," + strs[1] + "," + strs[2]);
    }
}

 */

/*
import java.util.HashSet;
import java.util.Iterator;

public class Example02 {
    public static void main(String[] args) {
        HashSet hset = new HashSet();   // 创建HashSet集合
        hset.add("张三");                  // 向该Set集合中添加字符串
        hset.add("李四");
        hset.add("王五");
        hset.add("李四");                  // 向该Set集合中添加重复元素
        Iterator it = hset.iterator();  // 获取Iterator对象
        while (it.hasNext()) {          // 通过while循环，判断集合中是否有元素
            Object obj = it.next();// 如果有元素，就调用迭代器的next()方法获取元素
            System.out.println(obj);
        }
    }
}

 */

/*
import java.util.HashSet;
import java.util.Iterator;

public class Example02 {
    public static void main(String[] args){
        HashSet hset=new HashSet();
        hset.add("张三");                  // 向该Set集合中添加字符串
        hset.add("李四");
        hset.add("王五");
        hset.add("李四");                  // 向该Set集合中添加重复元素
        Iterator it=hset.iterator();      //获取Iterator对象
        while(it.hasNext()){
            Object obj=it.next();
            System.out.println(obj);
        }
    }
}


 */

/*
import java.util.HashSet;

class Student {
    String id;
    String name;

    public Student(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String toString() {
        return id + ":" + name;
    }
}

public class Example02 {
    public static void main(String[] args) {
        HashSet hs = new HashSet();
        Student stu1 = new Student("1", "张三");
        Student stu2 = new Student("2", "李四");
        Student stu3 = new Student("2", "李四");
        hs.add(stu1);
        hs.add(stu2);
        hs.add(stu3);
        System.out.println(hs);
    }
}

 */

/*
import java.util.HashSet;

class Student {
    String id;
    String name;

    public Student(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String toString() {
        return id + ":" + name;
    }

    //重写hashCode()方法
    public int hashCode() {
        return id.hashCode();   //返回id属性的哈希值
    }

    //重写equals()方法
    public boolean equals(Object obj) {
        if (this == obj)      // 判断是否是同一个对象
            return true;
        if (!(obj instanceof Student)) {  //判断对象是Student类型
            return false;
        }
        Student stu = (Student) obj;   //将对象类型转为Student
        boolean b = this.id.equals(stu.id);   //判断id值是否相同
        return b;
    }

}

public class Example02 {
    public static void main(String[] args) {
        HashSet hs = new HashSet();
        Student stu1 = new Student("1", "张三");
        Student stu2 = new Student("2", "李四");
        Student stu3 = new Student("2", "李四");
        hs.add(stu1);
        hs.add(stu2);
        hs.add(stu3);
        System.out.println(hs);
    }
}

 */

/*
import java.util.Iterator;
import java.util.LinkedHashSet;

public class Example02 {
    public static void main(String[] args) {
        LinkedHashSet set = new LinkedHashSet();
        set.add("张三");                   // 向该Set集合中添加字符串
        set.add("李四");
        set.add("王五");
        Iterator it = set.iterator();  // 获取Iterator对象
        while (it.hasNext()) {           // 通过while循环，判断集合中是否有元素
            Object obj = it.next();
            System.out.println(obj);
        }
    }
}
 */

/*

import java.util.TreeSet;

public class Example02 {
    public static void main(String[] args) {
        // 创建TreeSet集合
        TreeSet ts = new TreeSet();
        // 1、向TreeSet集合中添加元素
        ts.add(3);
        ts.add(29);
        ts.add(101);
        ts.add(21);
        System.out.println("创建的TreeSet集合为：" + ts);
        // 2、获取首尾元素
        System.out.println("TreeSet集合首元素为：" + ts.first());
        System.out.println("TreeSet集合尾部元素为：" + ts.last());
        // 3、比较并获取元素
        System.out.println("集合中小于或等于9的最大的一个元素为：" + ts.floor(9));
        System.out.println("集合中大于10的最小的一个元素为：" + ts.higher(10));
        System.out.println("集合中大于100的最小的一个元素为：" + ts.higher(100));
        // 4、删除元素
        Object first = ts.pollFirst();
        System.out.println("删除的第一个元素是：" + first);
        System.out.println("删除第一个元素后TreeSet集合变为：" + ts);
    }
}

 */


/*
import java.util.TreeSet;

//定义Student类并实现Comparable接口，声明属性name和age，
// 重写toString()方法，
// 重写Comparable接口的compareTo()方法。
class Student implements Comparable {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String toString() {
        return name + ":" + age;
    }

    public int compareTo(Object obj) {
        Student stu = (Student) obj;
        //定义比较方式,先比较age,再比较name
        if (this.age > stu.age) {
            return 1;
        }
        if (this.age == stu.age) {      //age相同,比较name
            return this.name.compareTo(stu.name);
        }
        return -1;
    }
}

public class Example02 {
    public static void main(String[] args) {
        TreeSet ts = new TreeSet();
        ts.add(new Student("Lucy", 18));
        ts.add(new Student("Tom", 20));
        ts.add(new Student("Bob", 20));
        ts.add(new Student("Tom", 20));
        System.out.println(ts);
    }
}

 */


/*
import java.util.Comparator;
import java.util.TreeSet;

class Student {
    private String id;
    private String name;

    public Student(String id, String name) {
        this.id = id;
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public String getId() {
        return id;
    }

    @Override
    public String toString() {
        return id+":"+name;
    }
}
//在内部类中重写了Comparator接口的compare()方法。根据id升序,若id相同,根据name升序
public class Example02 {
    public static void main(String[] args) throws Exception {
        TreeSet ts = new TreeSet(new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                Student stu1 = (Student) o1;
                Student stu2 = (Student) o2;
                if (!stu1.getId().equals(stu2.getId())) {
                    return stu1.getId().compareTo(stu2.getId());
                } else {
                    return stu1.getName().compareTo(stu2.getName());
                }
            }
        });
        ts.add(new Student("2", "Mary"));//向ts集合中添加元素
        ts.add(new Student("1", "Jack"));
        ts.add(new Student("3", "Lisa"));
        ts.add(new Student("2", "Lily"));
        System.out.println(ts);
    }
}

 */

/*
import java.util.HashMap;
import java.util.Map;

public class Example02 {
    public static void main(String[] args) {
        Map map = new HashMap(); // 创建HashMap对象
        map.put("1", "张三");          // 存储键和值
        map.put("2", "李四");
        map.put("3", "王五");
        System.out.println("1：" + map.get("1"));  // 根据键获取值
        System.out.println("2：" + map.get("2"));
        System.out.println("3：" + map.get("3"));
    }
}

 */

/*
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class Example02 {
    public static void main(String[] args) {
        Map map = new HashMap();      // 创建HashMap集合
        map.put("1", "张三");               // 存储键和值
        map.put("2", "李四");
        map.put("3", "王五");
        Set keySet = map.keySet();         // 获取键的集合
        Iterator it = keySet.iterator();  // 获取Iterator对象
        while (it.hasNext()) {
            Object key = it.next();
            Object value = map.get(key);  // 获取每个键所对应的值
            System.out.println(key + ":" + value);
        }
    }
}

 */

/*
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

//先获取集合中所有的映射关系，然后从映射关系中取出键和值。
public class Example02 {
    public static void main(String[] args) {
        Map map = new HashMap();         // 创建HashMap集合
        map.put("1", "张三");       		// 存储键和值
        map.put("2", "李四");
        map.put("3", "王五");
        Set entrySet = map.entrySet();
        Iterator it = entrySet.iterator();              // 获取Iterator对象
        while (it.hasNext()) {
            // 获取集合中键值对映射关系
            Map.Entry entry = (Map.Entry) (it.next());
            Object key = entry.getKey();                // 获取Entry中的键
            Object value = entry.getValue();           // 获取Entry中的值
            System.out.println(key + ":" + value);
        }
    }
}

 */

/*
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class Example02 {
    public static void main(String[] args) {
        Map map = new HashMap();      // 创建HashMap集合
        map.put("1", "张三");     	 // 存储键和值
        map.put("3", "李四");
        map.put("2", "王五");
        map.put("4", "赵六");
        System.out.println("集合大小为："+map.size());
        System.out.println("判断是否包含传入的键（2）："+map.containsKey("2"));
        System.out.println("判断是否包含传入的值（王五）："+map.containsValue("王五"));
        System.out.println("移除键为1的值是："+map.remove("1"));
        Collection values = map.values();
        Iterator it = values.iterator();
        while (it.hasNext()) {
            Object value = it.next();
            System.out.println(value);
        }
    }
}

 */

/*
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class Example02 {
    public static void main(String[] args) {
        Map map = new LinkedHashMap();    //创建LinkedHashMap集合
        map.put("3", "李四");                         // 存储键和值
        map.put("2", "王五");
        map.put("4", "赵六");
        Set keySet=map.keySet();
        Iterator it=keySet.iterator();
        while(it.hasNext()){
            Object key=it.next();
            Object value=map.get(key);  //获取每个键所对应的值
            System.out.println(key+":"+value);
        }
    }
}

 */

/*
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class Example02 {
    public static void main(String[] args) {
        Map map = new TreeMap();      // 创建TreeMap集合
        map.put(3, "李四");                  // 存储键和值
        map.put(2, "王五");
        map.put(4, "赵六");
        map.put(3, "张三");
        Set keySet = map.keySet();
        Iterator it = keySet.iterator();
        while (it.hasNext()) {
            Object key = it.next();
            Object value = map.get(key); // 获取每个键所对应的值
            System.out.println(key+":"+value);
        }
    }
}

 */

/*
import java.util.Comparator;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;

class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student [name" + name + ", age=" + age + ']';
    }
}

public class Example02 {
    public static void main(String[] args) {
        TreeMap tm = new TreeMap(new Comparator<Student>() {
            @Override
            public int compare(Student s1, Student s2) {
                int num = s1.getName().compareTo(s2.getName());//按照姓名比较
                return num == 0 ? num:s1.getAge() - s2.getAge();
            }
        });
        tm.put(new Student("张三", 23), "北京");
        tm.put(new Student("李四", 13), "上海");
        tm.put(new Student("赵六", 43), "深圳");
        tm.put(new Student("王五", 33), "广州");
        Set keySet=tm.keySet();
        Iterator it=keySet.iterator();
        while(it.hasNext()){
            Object key=it.next();
            Object value=tm.get(key);   //获取每个键所对应的值
            System.out.println(key+":"+value);
        }
    }
}

 */

/*
import java.util.Enumeration;
import java.util.Properties;

public class Example02 {
    public static void main(String[] args){
        Properties p=new Properties();  //创建Properties对象
        p.setProperty("Backgroup-color", "red");
        p.setProperty("Font-size", "14px");
        p.setProperty("Language", "chinese");
        Enumeration names = p.propertyNames();  //获取Enumeration对象所有键枚举
        while(names.hasMoreElements()){     //循环遍历所有的键
            String key=(String)names.nextElement();
            String value=p.getProperty(key);    //获取对应键的值
            System.out.println("key="+key+" value="+value);
        }
    }
}

 */

/*
import java.util.ArrayList;
import java.util.Collections;

public class Example02 {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        Collections.addAll(list, "C", "Z", "B", "K");    // 添加元素
        System.out.println("排序前:" + list);
        Collections.reverse(list);
        System.out.println("反转后:" + list);
        Collections.sort(list);
        System.out.println("按自然顺序排序后: " + list);
        Collections.shuffle(list);                       // 随机打乱集合元素
        System.out.println("按随机顺序排序后:  " + list);
        Collections.swap(list, 0, list.size() - 1);// 将集合首尾元素交换
        System.out.println("集合首尾元素交换后: " + list);

    }
}

 */

/*
import java.util.ArrayList;
import java.util.Collections;

public class Example02{
    public static void main(String[] args){
        ArrayList<Integer> list = new ArrayList<>();
        Collections.addAll(list,-3,2,9,5,8);
        System.out.println("集合中的元素:"+list);
        System.out.println("集合中的最大元素: " + Collections.max(list));
        System.out.println("集合中的最小元素: " + Collections.min(list));
        Collections.replaceAll(list, 8, 0); // 将集合中的8用0替换掉
        System.out.println("替换后的集合: " + list);
        Collections.sort(list);               //使用二分查找前，必须保证元素有序
        System.out.println("集合排序后为： "+list);
        int index = Collections.binarySearch(list, 9);
        System.out.println("集合通过二分查找方法查找元素9所在索引为："+index);
    }
}

 */

/*
import java.util.Arrays;

public class Example02 {
    public static void main(String[] args) {
        int[] arr = {9, 8, 3, 5, 2};   // 初始化一个数组
        System.out.print("排序前：");
        printArray(arr);                    // 打印原数组
        Arrays.sort(arr);                   // 调用Arrays的sort()方法排序
        System.out.print("排序后：");
        printArray(arr);                    // 打印排序后数组
    }

    // 定义打印数组元素方法
    public static void printArray(int[] arr) {
        System.out.print("[");
        for (int x = 0; x < arr.length; x++) {
            if (x != arr.length - 1) {
                System.out.print(arr[x] + ", ");
            } else {
                System.out.println(arr[x] + "]");
            }
        }
    }

}

 */


/*
import java.util.Arrays;

public class Example02 {
    public static void main(String[] args) {
        int[] arr = { 9, 8, 3, 5, 2 };
        Arrays.sort(arr);                            // 对数组进行排序
        int index = Arrays.binarySearch(arr, 3); // 查找指定元素3
        System.out.println("元素3的索引是:" + index);
    }
}


 */

/*
import java.util.Arrays;

public class Example02 {
    public static void main(String[] args) {
        int[] arr = { 9, 8, 3, 5, 2 };
        // 复制一个指定范围的数组
        int[] copied = Arrays.copyOfRange(arr, 1, 7);
        for (int i = 0; i < copied.length; i++) {
            System.out.print(copied[i] + " ");
        }
    }
}

 */

/*
import java.util.Arrays;

public class Example02 {
    public static void main(String[] args) {
        int[] arr = { 1, 2, 3, 4 };
        Arrays.fill(arr, 8);       // 用8替换数组中的每个元素
        for (int i = 0; i < arr.length; i++) {
            System.out.println(i + ": " + arr[i]);
        }
    }
}

 */

import java.util.Arrays;

public class Example02 {
    public static void main(String[] args) {
        String[] arr = {"program", "creek", "is", "a", "java", "site"};
        Arrays.sort(arr, (m, n) -> Integer.compare(m.length(), n.length()));
        System.out.println("Lambda语句体中只有一条语句，参数类型可推断："+
                Arrays.toString(arr));
        Arrays.sort(arr, (String m, String n) -> {
            if (m.length() > n.length())
                return -1;
            else
                return 0;
        });
        System.out.println("Lambda语句体中有多条语句："+Arrays.toString(arr));
    }
}